<template>
  <div class="system-user-dialog-container">
    <el-dialog
      :title="state.dialog.title"
      v-model="state.dialog.isShowDialog"
      width="769px"
    >
      <el-form
        ref="interfaceDialogFormRef"
        :model="state.ruleForm"
        size="default"
        label-width="90px"
      >
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="接口名称">
              <el-input
                v-model="state.ruleForm.name"
                placeholder="请输入接口名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="接口地址">
              <el-input
                v-model="state.ruleForm.url"
                placeholder="请输入接口地址"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="接口类型">
              <el-select v-model="state.ruleForm.type" placeholder="请选择接口类型">
                <el-option
                  v-for="item in types"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="计费规则">
              <el-select
                v-model="state.ruleForm.charging_id"
                placeholder="请选择计费规则"
              >
                <el-option
                  v-for="item in charging_ids"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="版本">
              <el-input
                v-model="state.ruleForm.version"
                placeholder="请输入版本"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="状态">
              <el-switch
                v-model="state.ruleForm.status"
                inline-prompt
                active-text="启"
                inactive-text="禁"
                :active-value="1"
                :inactive-value="2"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="系统提示词">
              <el-input
                v-model="state.ruleForm.sys_prompt_tpl"
                placeholder="请输入系统提示词"
                clearable
                type="textarea"
                :rows="2"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="用户提示词">
              <el-input
                v-model="state.ruleForm.use_prompt_tpl"
                placeholder="请输入用户提示词"
                clearable
                type="textarea"
                :rows="4"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="接口描述">
              <el-input
                v-model="state.ruleForm.desc"
                placeholder="请输入接口描述"
                clearable
                type="textarea"
                :rows="2"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="备注">
              <el-input
                v-model="state.ruleForm.remark"
                placeholder="请输入备注"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit" size="default">{{
            state.dialog.submitTxt
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, nextTick } from "vue";

import { getAPI } from "/@/utils/axios-utils";
import { InterfaceApi, ComDataApi } from "/@/api-services/api";
import { InterfaceOut, ComDatasOut } from "/@/api-services/models";

// 定义子组件向父组件传值/事件
const emit = defineEmits(["refresh"]);

const types = [
  { id: "chat", name: "chat" },
  { id: "query", name: "query" },
  { id: "completion", name: "completion" },
  { id: "QA", name: "QA" },
];

const charging_ids = ref<ComDatasOut[]>([]);

const interfaceDialogFormRef = ref();
const state = reactive({
  ruleForm: {} as InterfaceOut,
  dialog: {
    isShowDialog: false,
    type: "",
    title: "",
    submitTxt: "",
  },
});

// 打开弹窗
const openDialog = (type: string, row: InterfaceOut) => {
  getComData();

  if (type === "edit") {
    state.ruleForm = row;
    state.dialog.title = "修改接口";
    state.dialog.submitTxt = "修 改";
  } else {
    state.dialog.title = "新增接口";
    state.dialog.submitTxt = "新 增";
    // 清空表单，此项需加表单验证才能使用
    state.ruleForm = {} as InterfaceOut;
    nextTick(() => {
      interfaceDialogFormRef.value.resetFields();
    });
  }
  state.dialog.isShowDialog = true;
  state.dialog.type = type;
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = () => {
  interfaceDialogFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return;

    if (state.dialog.type == "edit") {
      await getAPI(InterfaceApi).interfaceUpdate(state.ruleForm);
    } else {
      await getAPI(InterfaceApi).interfaceAdd(state.ruleForm);
    }
    closeDialog();
    emit("refresh");
  });
};

const getComData = async () => {
  const res = await getAPI(ComDataApi).getChargingList({});

  charging_ids.value.splice(0);
  res.data.data.forEach((item: any) => {
    charging_ids.value.push({
      id: item.id,
      name: item.name,
    });
  });
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
<style scoped lang="scss"></style>
